package seGobangClient.onlineHall;

import java.util.*;
import seGobangClient.Rule;
import GameHallClient.*;

/**
 *
 * @author plus
 */
public class Room {
    public int RoomID;
    public String RoomName, RoomDesc;
    public Rule MyRule;
    private OnlineHallController MyController;
    private Desk [] Deskes;
    Desk CurrentDesk=null;
    private Timer UpdateTimer=null;
    GameHallClient hall=new GameHallClient();
    
    public Room( OnlineHallController MyController,int RoomID, String RoomName, String RoomDesc, Rule rule) {
        this.MyController=MyController;
        this.RoomID = RoomID;
        this.RoomName = RoomName;
        this.RoomDesc = RoomDesc;
        this.MyRule=rule;
        Deskes=new Desk[13];
        for (int i=1; i<=12; i++)
            Deskes[i]=new Desk(MyController,this,i);
    }
    
    private void startStateUpdate(){
        if (UpdateTimer !=null) UpdateTimer.cancel();
        UpdateTimer=new Timer();
        UpdateTimer.schedule(new TimerTask(){
            /**
             * 玩家加入房间，开始刷新房间中棋桌的状态
             */
            public void run() {
                
                ArrayList<Integer> desk=hall.getFreeDeskList(RoomID);                
                
                for(int i=1;i<=12;i++){
                
                    Deskes[i].setState(desk.get(i-1));
                }
                
                MyController.doEvent("RefreshDeskState", Deskes);
                
                    /*int [] deskStates=WSGetDeskesStatesByRoom(RoomID, OnlineHallController.UID);
                    for (int i=1; i<=12; i++)
                        Deskes[i].setState(deskStates[i]);*/
               // MyController.doEvent("RefreshDeskState", Deskes[]);
            }
            
        }, 0, OnlineHallController.updateRate);
    }
    
    public void joinRoom(){
        MyController.doEvent("RoomView", this.RoomName);
        startStateUpdate();
    }
    
    public void exitRoom(){
        MyController.doEvent("RoomListView", this);
        if (UpdateTimer !=null) UpdateTimer.cancel();
        UpdateTimer=null;
    }
    
    public void pauseRoom(){
        if (UpdateTimer !=null) UpdateTimer.cancel();
        this.CurrentDesk.pauseDesk();
    }
    
    public void resumeRoom(){
        this.CurrentDesk.resumeDesk();
        startStateUpdate();
    }
    
    public void joinDesk(int DeskID){
        this.CurrentDesk=Deskes[DeskID];
        this.CurrentDesk.JoinDesk();
    }
    
    public void exitDesk(){
        this.CurrentDesk.ExitDesk();
    }
    
    public void getReady(){
        this.CurrentDesk.GetReady();
    }
    
    public void cancelReady(){
        this.CurrentDesk.CancelReady();
    }
}
